package com.liming.salesman.webapp.wicket.page;


import com.liming.core.web.wicket.BasePage;
import com.liming.salesman.api.biz.ISalesmanBeanService;
import com.liming.salesman.api.persistence.ISalesmanBean;
import com.liming.salesman.entities.SalesmanBeanImpl;
import org.apache.aries.blueprint.annotation.Reference;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormValidatingBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.time.Duration;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Salesman: sunlf
 * Date: 14-3-13
 * Time: 下午4:26
 * To change this template use File | Settings | File Templates.
 */
public class SalesmanPage extends BasePage {
    @Named
    @Inject
    @Reference(id = "salesmanBeanService", serviceInterface = ISalesmanBeanService.class)
    private ISalesmanBeanService salesmanBeanService;
    Label label1= new Label("pageName1","添加销售顾问");
    Label label2= new Label("pageName2","编辑销售顾问");


    public SalesmanPage() {
        label1.setVisible(true);
        label2.setVisible(false);
        add(label1);
        add(label2);

        final Form<ISalesmanBean> form = new Form<ISalesmanBean>("add_salesman_form", new CompoundPropertyModel<ISalesmanBean>(new SalesmanBeanImpl()));
        add(form);
        AjaxFormValidatingBehavior.addToAllFormComponents(form, "keydown", Duration.ONE_SECOND);

        TextField formName = new TextField("name");
        TextField formLoginName = new TextField("loginName");
        TextField formPassword = new TextField("password");
        TextField formEmail = new TextField("email");
        TextField formSex = new TextField("sex");
        TextField formDistributor = new TextField("distributor");

        form.add(formName);
        form.add(formLoginName);
        form.add(formPassword);
        form.add(formEmail);
        form.add(formSex);
        form.add(formDistributor);

        form.add(new AjaxButton("submit_button", form) {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> submitForm) {
                ISalesmanBean filledSalesmanBean = (ISalesmanBean) getParent().getDefaultModelObject();
                salesmanBeanService.saveEntity(filledSalesmanBean);
                setResponsePage(SalesmanPage.class);
                label1.setVisible(true);
                label2.setVisible(false);
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                System.out.println("onError");
            }
        });

        add(new Link("reset_salesman_1") {
            @Override
            public void onClick() {
                form.setModelObject(new SalesmanBeanImpl());
                label1.setVisible(true);
                label2.setVisible(false);
            }
        });

        form.add(new Link("reset_salesman_2") {
            @Override
            public void onClick() {
                form.setModelObject(new SalesmanBeanImpl());
                label1.setVisible(true);
                label2.setVisible(false);
            }
        });




        //List数据
        List<ISalesmanBean> persons = salesmanBeanService.getAllEntity();
        ListDataProvider<ISalesmanBean> listDataProvider = new ListDataProvider<ISalesmanBean>(persons);
        add(new DataView<ISalesmanBean>("userRows", listDataProvider) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final Item<ISalesmanBean> item) {
                final ISalesmanBean person;
                person = item.getModelObject();

                item.add(new Label("loginNameData", person.getLoginName()));
                item.add(new Label("passwordData", person.getPassword()));
                item.add(new Label("nameData", person.getName()));
                item.add(new Label("sexData", person.getSex()));
                item.add(new Label("emailData", person.getEmail()));
                item.add(new Label("distributorData", person.getDistributor()));
                Label enableLabel = new Label("isEnableData", person.isEnable() ? "有效" : "<font color='red'>无效</font>");
                //使html渲染生效
                enableLabel.setEscapeModelStrings(false);
                item.add(enableLabel);
                item.add(new Link("edit") {
                    @Override
                    public void onClick() {
                        form.setModelObject((ISalesmanBean) getParent().getDefaultModelObject());
                        label1.setVisible(false);
                        label2.setVisible(true);
                    }
                });
                item.add(new Link("del") {
                    @Override
                    public void onClick() {
                        salesmanBeanService.deleteEntity(person.getId());
                        setResponsePage(SalesmanPage.class);

                    }
                });

                item.add(AttributeModifier.replace("class", new AbstractReadOnlyModel<String>() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public String getObject() {
                        return (item.getIndex() % 2 == 1) ? "even" : "odd";
                    }
                }));
            }
        });


    }
}
